package cn.yj.relay.core.controller;


import cn.yj.relay.common.web.OrderBy;
import cn.yj.relay.core.entity.Emp;
import cn.yj.relay.core.service.IEmpService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.yj.relay.common.web.R;
import cn.yj.relay.common.exception.MyException;
import cn.yj.relay.common.web.BaseController;

import java.util.EnumMap;
import java.util.Map;

import org.springframework.web.bind.annotation.*;
import cn.yj.relay.core.entity.Task;
import cn.yj.relay.core.service.ITaskService;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Arrays;
import java.util.List;

import org.springframework.ui.Model;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 前端控制器
 * </p>
 *你看作者啊
 * 代码风格不一样
 * @author 永健
 * @since 2020-03-03 17:41
 */

@RestController
@RequestMapping("/task")
public class TaskController extends BaseController<Task>
{

    @Autowired
    ITaskService thisService;

    @Autowired
    IEmpService iEmpService;



    @GetMapping("/count")
    public R count(){
        String role = getRole();
        if (role.equals("admin")){
            return success(thisService.countDataAllUser());
        }
        return success(thisService.countDataByEmpId(getLoginEmp().getId()));
    }


    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> param)
    {
        if (getRole().equals("admin"))
        {
            return success(thisService.findList(param, page()));
        }
        param.put("empId", getLoginEmp().getId());
        return success(thisService.findList(param, page(new OrderBy(OrderBy.Direction.DESC, "create_time"))));
    }


    @PostMapping("/save")
    public R insertSave(@Valid Task entity)
    {
        Integer empId = entity.getEmpId();
        Emp emp = iEmpService.getById(empId);
        entity.setEmpName(emp.getName());
        entity.setEmpPhone(emp.getPhone());

        // 算提成吧
        Double point = entity.getRoyaltyPoint();
        Double allMoney = entity.getAllMoney();

        entity.setRoyaltyMoney(Math.floor(allMoney * point));


        int count = thisService.count(new QueryWrapper<Task>().lambda().eq(Task::getOutNum, entity.getOutNum()));
        if (count>0){
            throw new MyException("流水号是唯一的");
        }


        return result(thisService.save(entity));
    }


    @PutMapping("/update")
    public R editSave(@Valid Task entity)
    {
        Task task = thisService.getById(entity.getId());
        entity.setEmpName(task.getEmpName());
        entity.setEmpPhone(task.getEmpPhone());

        // 算提成吧
        Double point = entity.getRoyaltyPoint();

        Double allMoney = entity.getAllMoney();

        entity.setRoyaltyMoney(Math.floor(allMoney * point));

        return result(thisService.updateById(entity));
    }

    @GetMapping("/selectById/{id}")
    public R selectById(@PathVariable("id") String id)
    {
        return success(thisService.getById(id));
    }


    @DeleteMapping("/remove/{ids}")
    public R delete(@PathVariable("ids") List<Integer> ids)
    {
        return result(thisService.removeByIds(ids));
    }
}

